package io.plus.interactive.user_interface;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicLong;

@Data
public class Packet implements java.io.Serializable, Cloneable {
    private static final long serialVersionUID = 5275372187150637318L;
    private static final AtomicLong ID_ATOMICLONG = new AtomicLong();
    private static Logger log = LoggerFactory.getLogger(Packet.class);
    private Long id = ID_ATOMICLONG.incrementAndGet();
    /**
     * 本packet在解码时，消耗的字节数
     */
    private int byteCount = 0;
    private Long respId = null;

    private boolean isBlockSend = false;
    private PacketTag packetTag = null;
    /**
     * 消息是否是另外一台机器通过topic转过来的，如果是就不要死循环地再一次转发啦
     * 这个属性是tio内部使用，业务层的用户请勿使用
     */
    private boolean isFromCluster = false;
    /**
     * 同步发送时，需要的同步序列号
     */
    private Integer synchronismSerialNumber = 0;
    /**
     * 预编码过的bytebuffer，如果此值不为null，框架则会忽略原来的encode()而直接用此值
     */
    private ByteBuffer preEncodedByteBuffer = null;


    @Override
    public Packet clone() {
        try {
            Packet ret = (Packet) super.clone();
            ret.setPreEncodedByteBuffer(null);

            return ret;
        } catch (CloneNotSupportedException e) {
            log.error("", e);
            return null;
        }
    }


}
